#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion

using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using Newtonsoft.Utilities;
using System.Reflection;
using Newtonsoft.Utilities.Miscellaneous;
using Newtonsoft.Utilities.Testing;

namespace Newtonsoft.Utilities.Tests.Miscellaneous
{
  public class DataMapperTests : TestsBase
  {
    [Test]
    public void NumericTypes()
    {
      DataMapTarget target = new DataMapTarget();

      DataMapper.SetPropertyValue(target, "MyInt", 42);
      Assert.AreEqual(target.MyInt, 42, "Int should match");

      DataMapper.SetPropertyValue(target, "MyDouble", 4.2);
      Assert.AreEqual(target.MyDouble, 4.2, "Double should match");

      DataMapper.SetPropertyValue(target, "MyBool", true);
      Assert.AreEqual(target.MyBool, true, "Bool should be true");

      DataMapper.SetPropertyValue(target, "MyBool", false);
      Assert.AreEqual(target.MyBool, false, "Bool should be false");
    }

    [Test]
    public void NullableTypes()
    {
      DataMapTarget target = new DataMapTarget();

      DataMapper.SetPropertyValue(target, "MyNInt", 42);
      Assert.AreEqual(target.MyNInt, 42, "Int should match");

      DataMapper.SetPropertyValue(target, "MyNInt", null);
      Assert.AreEqual(target.MyNInt, null, "Int should be null");
    }

    [Test]
    public void EnumTypes()
    {
      DataMapTarget target = new DataMapTarget();

      DataMapper.SetPropertyValue(target, "MyEnum", DataMapEnum.Second);
      Assert.AreEqual(target.MyEnum, DataMapEnum.Second, "Enum should be Second");

      DataMapper.SetPropertyValue(target, "MyEnum", "First");
      Assert.AreEqual(target.MyEnum, DataMapEnum.First, "Enum should be First");
    }

    [Test]
    public void MapObject()
    {
      DataMapTarget source = new DataMapTarget();
      source.MyBool = true;
      source.MyString = "Pie";

      DataMapTarget destination = new DataMapTarget();

      DataMapper.Map(source, destination);

      Assert.AreEqual(source.MyBool, destination.MyBool);
      Assert.AreEqual(source.MyString, destination.MyString);
    }

    [Test]
    public void MapInheritedObject()
    {
      InheritedDataMapTarget source = new InheritedDataMapTarget();
      source.MyBool = true;
      source.MyString = "Pie";
      source.InheritedString = "InheritedString";

      InheritedDataMapTarget destination = new InheritedDataMapTarget();

      DataMapper.Map(source, destination);

      Assert.AreEqual(source.MyBool, destination.MyBool);
      Assert.AreEqual(source.MyString, destination.MyString);
      Assert.AreEqual(source.InheritedString, destination.InheritedString);
    }

    [Test]
    public void MapFields()
    {
      DataMapTarget source = new DataMapTarget();
      source.MyBool = true;
      source.MyString = "Pie";

      DataMapTargetFields destination = new DataMapTargetFields();

      DataMapper.Map(source,
        destination,
        false,
        MemberTypes.Field, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
        "_nint",
        "_enum");

      Assert.AreEqual(source.MyBool, destination.Bool);
      Assert.AreEqual(source.MyDouble, destination.Double);
      Assert.AreEqual(source.MyString, destination.String);
      Assert.AreEqual(source.MyInt, destination.Int);
    }
  }

  public enum DataMapEnum
  {
    First,
    Second,
    Third
  }

  public class DataMapTargetFields
  {
    private int _int;
    private double _double;
    private bool _bool;
    private string _string;

    public string PublicString;

    public int Int
    {
      get { return _int; }
      set { _int = value; }
    }

    public double Double
    {
      get { return _double; }
      set { _double = value; }
    }

    public bool Bool
    {
      get { return _bool; }
      set { _bool = value; }
    }

    public string String
    {
      get { return _string; }
      set { _string = value; }
    }
  }

  public class InheritedDataMapTarget : DataMapTarget
  {
    private string _inheritedString;

    public string InheritedString
    {
      get { return _inheritedString; }
      set { _inheritedString = value; }
    }
  }

  public class DataMapTarget
  {
    private int _int;
    private double _double;
    private bool _bool;
    private Nullable<int> _nint;
    private DataMapEnum _enum;
    private string _string;

    public int MyInt
    {
      get { return _int; }
      set { _int = value; }
    }

    public double MyDouble
    {
      get { return _double; }
      set { _double = value; }
    }

    public bool MyBool
    {
      get { return _bool; }
      set { _bool = value; }
    }

    public Nullable<int> MyNInt
    {
      get { return _nint; }
      set { _nint = value; }
    }

    public DataMapEnum MyEnum
    {
      get { return _enum; }
      set { _enum = value; }
    }

    public string MyString
    {
      get { return _string; }
      set { _string = value; }
    }
  }
}